/**
 * Classname :DBUtils.java
 *
 * Version information: 1.0
 *
 * Date: Dec 27, 2010 ho.viet.duan create
 *
 * Planex Viet Name co.ltd, All Rights Reserved.
 */

package com.zho.common.utility;

import java.util.Date;

import com.zho.common.domain.db.PagingInfo;

import org.hibernate.Query;


public abstract class DBUtils {
    public static final String WILDCARD = "%";

    /**
     * 
     * @param str
     * @return
     */
    public static String applyWildcard(String str) {
        str = (str == null ? "" : str.trim());
        return (str.endsWith(WILDCARD) ? str : str + WILDCARD);
    }

    /**
     * 
     * @param str
     * @return
     */
    public static String applyFreeSearch(String str) {
        StringBuilder strBuf = new StringBuilder();
        str = (str == null ? "" : str.trim());
        strBuf.append(str.startsWith(WILDCARD) ? "" : WILDCARD).append(str).append(str.endsWith(WILDCARD) ? "" : WILDCARD);
        return strBuf.toString();
    }

    /**
     * 
     * @param query
     * @param pagingInfo
     */
    public static void addPaging(Query query, PagingInfo pagingInfo) {
        if (query != null && pagingInfo != null) {
            query.setFirstResult(pagingInfo.getIndex());
            query.setMaxResults(pagingInfo.getOffset());
        }
    }

    /**
     * 
     * @param pagingInfo
     * @param objTotal
     */
    public static void setTotalValue(PagingInfo pagingInfo, Object objTotal) {
        if (pagingInfo != null && objTotal != null) {
            if (objTotal instanceof Long) {
                pagingInfo.setTotal(((Long) objTotal).longValue());
            } else if (objTotal instanceof Integer) {
                pagingInfo.setTotal(((Integer) objTotal).longValue());
            }
        }
    }

    /**
     * 
     * @param date
     * @param defaultDate
     * @return
     */
    public static Date getDate(Date date, Date defaultDate) {
        return (date == null ? defaultDate : date);
    }

    /**
     * 
     * @param value
     * @param defaultValue
     * @return
     */
    public static Integer getNumber(Integer value, Integer defaultValue) {
        return (value == null ? defaultValue : value);
    }

    /**
     * 
     * @param value
     * @param defaultValue
     * @return
     */
    public static Long getNumber(Long value, Long defaultValue) {
        return (value == null ? defaultValue : value);
    }

    /**
     * 
     * @param value
     * @param defaultValue
     * @return
     */
    public static Double getNumber(Double value, Double defaultValue) {
        return (value == null ? defaultValue : value);
    }
}
